package com.mayikt.manage;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.entity.UnionLoginDO;
import com.mayikt.entity.UserLoginLogDo;
import com.mayikt.mapper.UnionLoginMapper;
import com.mayikt.mapper.UserLoginLogMapper;
import com.mayikt.request.LoginTemplateDTO;
import com.mayikt.service.feign.WeChatLoginTemplateFeign;
import com.mayikt.strategy.UnionLoginStrategy;
import com.mayikt.utils.DesensitizationUtil;
import com.mayikt.utils.SpringContextUtils;
import com.mayikt.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Name AsyncLoginLogManage
 * @Author 枫火
 * @Date 2021/5/21 9:36
 * @Description
 **/
@Component
@Slf4j
public class AsyncLoginLogManage {

    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    @Autowired
    private UnionLoginMapper unionLoginMapper;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private WeChatLoginTemplateFeign weChatLoginTemplateFeign;

    @Async
    public void loginLog(String qqOpenIdToken, String openId, String mobile, Long userId, String loginIp, Date loginTime, String loginToken, String channel,
                         String equipment) {
        // 1. 根据当前的渠道+userid+可用 查询 该用户是否已经登录过
        UserLoginLogDo userLoginLogDo =
                userLoginLogMapper.selectByUserIdAndLoginType(userId, channel);
        // 2. 如果没有查询到记录情况下
        if (userLoginLogDo != null) {
            String oldToken = userLoginLogDo.getLoginToken();
            // 更新数据库token状态
            userLoginLogMapper.updateUserTokenNotQuipment(oldToken);
            // 从redis删除该token
            tokenUtil.delToken(oldToken);
        }
        // 插入最新的token到数据库中
        UserLoginLogDo newUserLoginLogDo = new UserLoginLogDo(userId, loginIp, loginTime, loginToken, channel, equipment);
        userLoginLogMapper.insertUserLoginLog(newUserLoginLogDo);

        // 3. 发送登录日志给微信客户端 调用微信接口发送消息模板
//        if (!StringUtils.isEmpty(openId)) {
//            LoginTemplateDTO loginTemplateDto = new
//                    LoginTemplateDTO(DesensitizationUtil.mobileEncrypt(mobile),
//                    loginTime, loginIp, equipment, openId);
//            weChatLoginTemplateFeign.sendLoginTemplate(loginTemplateDto);
//        }

        // 4.关联openid
        if (!StringUtils.isEmpty(qqOpenIdToken)) {
            String openIdJson = tokenUtil.getTokenValue(qqOpenIdToken);
            JSONObject jsonObject = JSONObject.parseObject(openIdJson);
            String unionPublicId = jsonObject.getString("unionPublicId");
            // 3.根据该渠道id查询bean的id ，从容器中获取
            UnionLoginDO unionLoginDo = unionLoginMapper.selectByUnionLoginId(unionPublicId);
            if (unionLoginDo == null) {
                return;
            }
            String unionBeanId = unionLoginDo.getUnionBeanId();
            UnionLoginStrategy unionLoginStrategy = SpringContextUtils.
                    getBean(unionBeanId, UnionLoginStrategy.class);
            if (unionLoginStrategy == null) {
                return;
            }
            String tempOpenId = jsonObject.getString("openId");
            unionLoginStrategy.updateUseOpenId(userId, tempOpenId);
        }
    }
}
